package com.jinwei.qunguanjia.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.domain.model.Sort;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dao.UserVerificationCodeDAO;
import com.jinwei.qunguanjia.user.dto.UserVerificationCodeDTO;
import com.jinwei.qunguanjia.user.po.UserVerificationCodePO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("userVerificationCodeService")
public class UserVerificationCodeServiceImpl implements UserVerificationCodeService {

    @Resource
    private UserVerificationCodeDAO userVerificationCodeDAO;

    @Override
    public String save(UserVerificationCodeDTO userVerificationCodeDTO) {
        UserVerificationCodePO userVerificationCodePO = new UserVerificationCodePO();
        BeanUtils.copyProperties(userVerificationCodeDTO, userVerificationCodePO);
        String verificationCodeId = IdUtils.getWorkerId();
        userVerificationCodePO.setVerificationCodeId(verificationCodeId);
        userVerificationCodePO.setCreateTime(DateUtils.getDateTime());
        userVerificationCodeDAO.insert(userVerificationCodePO);
        return verificationCodeId;
    }

    @Override
    public void remove(String id) {
        userVerificationCodeDAO.deleteById(id);
    }

    @Override
    public void update(UserVerificationCodeDTO userVerificationCodeDTO) {
        UserVerificationCodePO userVerificationCodePO = new UserVerificationCodePO();
        BeanUtils.copyProperties(userVerificationCodeDTO, userVerificationCodePO);
        userVerificationCodeDAO.updateById(userVerificationCodePO);
    }

    @Override
    public UserVerificationCodeDTO get(String id) {
        UserVerificationCodePO userVerificationCodePO = userVerificationCodeDAO.selectById(id);
        if (null == userVerificationCodePO) {
            return null;
        }
        UserVerificationCodeDTO userVerificationCodeDTO = new UserVerificationCodeDTO();
        BeanUtils.copyProperties(userVerificationCodePO, userVerificationCodeDTO);
        return userVerificationCodeDTO;
    }

    @Override
    public UserVerificationCodeDTO get(UserVerificationCodeDTO userVerificationCodeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userVerificationCodeDTO);
        // 拼接查询条件
        QueryWrapper<UserVerificationCodePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("create_time",Sort.DESC));
        SearchUtils.parseOrderBySql(queryWrapper, sortList);
        SearchUtils.parseLimitSql(queryWrapper,0, 1);
        UserVerificationCodePO userVerificationCodePO = userVerificationCodeDAO.selectOne(queryWrapper);
        if (null == userVerificationCodePO) {
            return null;
        }
        BeanUtils.copyProperties(userVerificationCodePO, userVerificationCodeDTO);
        return userVerificationCodeDTO;
    }

    @Override
    public Long count(UserVerificationCodeDTO userVerificationCodeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userVerificationCodeDTO);
        // 拼接查询条件
        QueryWrapper<UserVerificationCodePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return userVerificationCodeDAO.selectCount(queryWrapper);
    }

    @Override
    public List<UserVerificationCodeDTO> list(UserVerificationCodeDTO userVerificationCodeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userVerificationCodeDTO);
        // 拼接查询条件
        QueryWrapper<UserVerificationCodePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(userVerificationCodeDAO.selectList(queryWrapper), UserVerificationCodeDTO.class);
    }

    @Override
    public PageDataInfo<UserVerificationCodeDTO> listPage(UserVerificationCodeDTO userVerificationCodeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userVerificationCodeDTO);
        // 拼接查询条件
        QueryWrapper<UserVerificationCodePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<UserVerificationCodePO> list = userVerificationCodeDAO.selectList(queryWrapper);
        PageInfo<UserVerificationCodePO> pageInfo = new PageInfo<>(list);
        PageDataInfo<UserVerificationCodeDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, UserVerificationCodeDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        return pageDataInfo;
    }

    @Override
    public String checkUnique(UserVerificationCodeDTO userVerificationCodeDTO) throws IllegalAccessException {
        String userVerificationCodeId = StringUtils.isNull(userVerificationCodeDTO.getVerificationCodeId()) ? "-1" : userVerificationCodeDTO.getVerificationCodeId();
        UserVerificationCodeDTO info = get(userVerificationCodeDTO);
        if (StringUtils.isNotNull(info) && !info.getVerificationCodeId().equals(userVerificationCodeId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
